package com.ricchieri.manage.bean;

import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.primefaces.context.RequestContext;
import org.primefaces.event.FlowEvent;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.UserFilter;
import com.ricchieri.lazymodel.UserLazyDataModel;
import com.ricchieri.service.interf.ICancelationMotiveService;
import com.ricchieri.service.interf.IDocumentTypeService;
import com.ricchieri.service.interf.IUserService;
import com.ricchieri.util.FacesUtil;
import com.ricchieri.util.StringUtils;

@ManagedBean(name = "cancelMultipleReservationMB")
@ViewScoped
public class CancelMultipleReservationManageBean extends CommonCancelMultipleReservationManageBean {

	private static final long serialVersionUID = 5531296565698443473L;
	
	@ManagedProperty(value = "#{CancelationMotiveService}")
	private ICancelationMotiveService cancelationMotiveService;
	
	@ManagedProperty(value = "#{UserService}")
	private IUserService userService;
	
	@ManagedProperty(value = "#{DocumentTypeService}")
	private IDocumentTypeService documentTypeService;

	private Map<String, String> cancelationTypes;
	private Map<String, String> trips;
	private Map<String, String> reservationTypes;
	private Map<String, String> passengerTypes;
	private Map<String, String> documents;
	
	private UserFilter userFilter = new UserFilter();
	private UserLazyDataModel userLazyDataModel;

	public ICancelationMotiveService getCancelationMotiveService() {
		return cancelationMotiveService;
	}

	public void setCancelationMotiveService(
			ICancelationMotiveService cancelationMotiveService) {
		this.cancelationMotiveService = cancelationMotiveService;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public Map<String, String> getTrips() {
		if (trips == null) {
			trips = getTripService().getComboTrips();
		}

		return trips;
	}
	
	public Map<String, String> getReservationTypes() {
		if (reservationTypes == null){
			reservationTypes = getReservationTypeService().getComboReservationType();
		}
	
		return reservationTypes;		
	}
	
	public Map<String, String> getCancelationTypes() {
		if(cancelationTypes == null) {
			cancelationTypes = getCancelationTypeService().getComboCancelationTypes();
		}
		
		return cancelationTypes;
	}
	
	public Map<String, String> getPassengerTypes() {
		if (passengerTypes == null){
			passengerTypes = getPassengerTypeService().getComboPassengerType();
		}
		return passengerTypes;		
	}

	public void search() {
		userLazyDataModel = null;
	}
	
	public void reset() {
		userFilter = new UserFilter();
		userLazyDataModel = null;
	}
	
	public UserFilter getUserFilter() {
		return userFilter;
	}
	
	public void setUserFilter(UserFilter userFilter) {
		this.userFilter = userFilter;
	}
	
	public UserLazyDataModel getUserLazyDataModel() {
		if(userLazyDataModel == null) {
			userLazyDataModel = new UserLazyDataModel(userService, userFilter);
		}
		return userLazyDataModel;
	}

	public void setUserLazyDataModel(UserLazyDataModel userLazyDataModel) {
		this.userLazyDataModel = userLazyDataModel;
	}
	
	public void setDocuments(Map<String, String> documents) {
		this.documents = documents;
	}

	public Map<String, String> getDocuments() {
		if(documents == null) {
			documents = this.getDocumentTypeService().getComboDocuments();
		}
		return documents;
	}

	public IDocumentTypeService getDocumentTypeService() {
		return documentTypeService;
	}

	public void setDocumentTypeService(IDocumentTypeService documentTypeService) {
		this.documentTypeService = documentTypeService;
	}
	
	
	public Boolean validateTravelInformation() {
		RequestContext context = RequestContext.getCurrentInstance();
		Boolean isValid = Boolean.TRUE;
		
		if(getCancelMultipleReservationBean().getFinishDate() != null &&
				getCancelMultipleReservationBean().getStartDate()  != null &&
				getCancelMultipleReservationBean().getFinishDate().before(getCancelMultipleReservationBean().getStartDate())) {
			FacesUtil.showFatalMessage("fixedMultipleReservationForm", "dateRangeWrong");
			isValid = Boolean.FALSE;
		}
		
		if(getCancelMultipleReservationBean().getBlock() && StringUtils.isEmpty(this.getCancelMultipleReservationBean().getBlockMotive())) {
			FacesUtil.addErrorMsg("fixedMultipleReservationForm:blockMotive", "requiredBlockMotive");
			isValid = Boolean.FALSE;
		}
		
		if(getCancelMultipleReservationBean().getSelectedDays().isEmpty()) {
			FacesUtil.showFatalMessage("fixedMultipleReservationForm", "requiredDayWeek");
			isValid = Boolean.FALSE;
		}
		
		context.addCallbackParam("isValid", isValid);
		return isValid;
	}
	
	public String onFlowProcess(FlowEvent event) {  
		return event.getNewStep();
    }

	public String confirmCancelation() {
		getCancelationMotiveService().addMultipleCancelation(this.getCancelMultipleReservationBean(), this.getSelectedUser());
		
		FacesUtil.addParametersFlash(BusinessSettings.KEY_USER, getSelectedUser());
		FacesUtil.addParametersFlash(BusinessSettings.KEY_MULTIPLE_CANCELATION, this.getCancelMultipleReservationBean());
		
		return BusinessSettings.SUCCESS;
	}
}
